package tree.leveltraversal;

import tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Classname : FindBottomLeftTreeValue
 * @Description :
 * <a href="https://leetcode.cn/problems/find-bottom-left-tree-value/">513. 找树左下角的值</a>
 * @Author : chentianyu
 * @Date 2023/1/21 09:01
 */


public class FindBottomLeftTreeValue {

    /**
     * 队列实现广度优先搜索
     *
     * @param root
     * @return
     */
    public int findBottomLeftValue(TreeNode root) {
        int bottomLeftValue = root.val;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            bottomLeftValue = q.peek().val;
            for (int i = 0; i < size; i++) {
                TreeNode cur = q.poll();
                if (cur.left != null) q.offer(cur.left);
                if (cur.right != null) q.offer(cur.right);
            }
        }
        return bottomLeftValue;
    }
}
